package com.bestcem.xm.common.core.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * restTemplate 配置
 *
 * @ClassName RestConfig
 * @Author handsome
 * @Date 2019/10/25 17:40
 * @Version V1.0
 **/
@Data
@Configuration
public class RestConfig {
    @Value("${restConfig.timeOut:5000}")
    private int restTimeOut;

    @Bean(name = "restTemplate")
    public RestTemplate getRestTemplate() {
        // 复杂构造函数的使用
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        // 设置超时
        requestFactory.setConnectTimeout(restTimeOut);
        requestFactory.setReadTimeout(restTimeOut);

        // 利用复杂构造器可以实现超时设置，内部实际实现为 HttpClient
        return new RestTemplate(requestFactory);
    }

}
